
var objects = {};

exports.listen = function(port){
  var io = require('socket.io').listen(port);
  io.sockets.on('connection', function (socket) {
    socket.on('jsr.get', function(id, fn){
      var obj = objects[id];
      if(!obj){
        obj = objects[id] = {};
      }
      fn(obj);
    });
  
    socket.on('jsr.change', function(param){
      var id = param.id;
      var name = param.name;
      var value = param.value;
      var o = objects[id];
      if(o[name] != value){
        o[name] = value;      
        socket.broadcast.emit('jsr.onchange.' + id, {
          name:name, 
          value:value
        });
      }
    });
    
    socket.on('jsr.method', function(param){
      socket.broadcast.emit('jsr.onmethod.' + param.id, {
        method:param.method,
        args:param.args
      });
    });
  });
};

